package com.xiaozhen.data.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xiaozhen.data.config.InfluxDBConnect;
import com.xiaozhen.data.service.JT809ProcessingService;
import com.xiaozhen.data.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * HISTORY
 *
 * @author zhenweisheng
 * @date 2021/9/23 15:23
 * @description:
 */
@Service
public class JT809ProcessingServiceImpl implements JT809ProcessingService {

    @Autowired
    private InfluxDBConnect influxDBConnect;

    @Override
    public void saveUpExgLocationData(JSONObject sourceJson) {
        JSONArray resultJSONArray = new JSONArray();
        resultJSONArray.add(new JSONObject());
        JSONArray fields = setFields(sourceJson, resultJSONArray);
        int size = fields.size();
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            list.add(fields.getJSONObject(i));
        }
        // tags 是建立索引要查询的字段
        Map<String, String> tags = new HashMap<>();
        //车牌号
        tags.put("vehicleNo", list.get(0).get("vehicleNo").toString());
        for (int i = 0; i < size; i++) {
            Map<String, Object> fieldMap = list.get(i);
            fieldMap.remove("data");
            fieldMap.remove("vehicleNo");
            Object dateTime = fieldMap.remove("dateTime");
            if (dateTime != null) {
                fieldMap.put("currentTime", DateUtil.parseMicTime(dateTime.toString()));
            }
        }
        influxDBConnect.batchInsert("upExgRealLocation", tags, list);
    }

    @Override
    public void saveUpExgRegister(JSONObject sourceJson) {
        JSONArray resultJSONArray = new JSONArray();
        resultJSONArray.add(new JSONObject());
        JSONArray fields = setFields(sourceJson, resultJSONArray);
        int size = fields.size();
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            list.add(fields.getJSONObject(i));
        }
        // tags 是建立索引要查询的字段
        Map<String, String> tags = new HashMap<>();
        //车牌号
        tags.put("vehicleNo", list.get(0).get("vehicleNo").toString());
        for (int i = 0; i < size; i++) {
            list.get(i).remove("data");
            list.get(i).remove("vehicleNo");
        }
        influxDBConnect.batchInsert("upExgRegister", tags, list);
    }

    @Override
    public void saveUpWarnUrge(JSONObject sourceJson) {
        JSONArray resultJSONArray = new JSONArray();
        resultJSONArray.add(new JSONObject());
        JSONArray fields = setFields(sourceJson, resultJSONArray);
        int size = fields.size();
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            list.add(fields.getJSONObject(i));
        }
        // tags 是建立索引要查询的字段
        Map<String, String> tags = new HashMap<>();
        //车牌号
        tags.put("vehicleNo", list.get(0).get("vehicleNo").toString());
        for (int i = 0; i < size; i++) {
            list.get(i).remove("data");
            list.get(i).remove("vehicleNo");
        }
        influxDBConnect.batchInsert("upWarnUrge", tags, list);
    }

    @Override
    public void saveUpWarnAdpt(JSONObject sourceJson) {
        JSONArray resultJSONArray = new JSONArray();
        resultJSONArray.add(new JSONObject());
        JSONArray fields = setFields(sourceJson, resultJSONArray);
        int size = fields.size();
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            list.add(fields.getJSONObject(i));
        }
        // tags 是建立索引要查询的字段
        Map<String, String> tags = new HashMap<>();
        //车牌号
        tags.put("vehicleNo", list.get(0).get("vehicleNo").toString());
        for (int i = 0; i < size; i++) {
            Map<String, Object> fieldMap = list.get(i);
            fieldMap.remove("data");
            fieldMap.remove("vehicleNo");
            Object warnTime = fieldMap.remove("warnTime");
            if (warnTime != null) {
                fieldMap.put("currentTime", DateUtil.parseMicTime(warnTime.toString()));
            }
        }
        influxDBConnect.batchInsert("upWarnAdpt", tags, list);
    }

    public JSONArray setFields(JSONObject sourceJson, JSONArray resultJSONArray) {
        Set<Map.Entry<String, Object>> entries = sourceJson.entrySet();
        Iterator<Map.Entry<String, Object>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> next = iterator.next();
            Object value = next.getValue();
            if (value instanceof JSONObject) {
                setFields((JSONObject) value, resultJSONArray);
            } else if (value instanceof JSONArray) {
                JSONArray values = (JSONArray) value;
                String jsonString = JSON.toJSONString(resultJSONArray);
                resultJSONArray.clear();
                int size = values.size();
                for (int i = 0; i < size; i++) {
                    JSONObject jsonObject = values.getJSONObject(i);
                    JSONArray jsonArray = setFields(jsonObject, JSON.parseArray(jsonString));
                    for (int j = 0; j < jsonArray.size(); j++) {
                        resultJSONArray.add(jsonArray.getJSONObject(j));
                    }
                }
            } else {
                String key = next.getKey();
                int size = resultJSONArray.size();
                for (int i = 0; i < size; i++) {
                    JSONObject jsonObject = resultJSONArray.getJSONObject(i);
                    jsonObject.put(key, value);
                }
            }
        }
        return resultJSONArray;
    }
}
